package Views;

import javax.swing.JTabbedPane;

public class SearchableTabbedPane extends JTabbedPane
{
	/**
	 * Instantierer tabpane'en og indsætter tabs i den
	 */
	public SearchableTabbedPane()
	{
		TabIndex tab = TabIndex.Customers;
		this.insertTab(tab.getTitle(), tab.getImage(), new CustomerPanel(this), null, tab.getIndex());
		tab = TabIndex.Locations;
		this.insertTab(tab.getTitle(), tab.getImage(), new LocationPanel(this), null, tab.getIndex());
		tab = TabIndex.Routes;
		this.insertTab(tab.getTitle(), tab.getImage(), new RoutePanel(this), null, tab.getIndex());
		tab = TabIndex.Rounds;
		this.insertTab(tab.getTitle(), tab.getImage(), new RoundPanel(this), null, tab.getIndex());
		tab = TabIndex.Employees;
		this.insertTab(tab.getTitle(), tab.getImage(), new EmployeePanel(this), null, tab.getIndex());
		tab = TabIndex.Cars;
		this.insertTab(tab.getTitle(), tab.getImage(), new CarPanel(this), null, tab.getIndex());
		tab = TabIndex.Schedules;
		this.insertTab(tab.getTitle(), tab.getImage(), new SchedulePanel(this), null, tab.getIndex());
		tab = TabIndex.Search;
		this.insertTab(tab.getTitle(), tab.getImage(), new SearchPanel(this), null, tab.getIndex());
	}

	/**
	 * Skifter til søge-tabben og starter søgningen
	 * @param section TabIndexet for sektionen
	 * @param searchText Søgetekst
	 */
	public void search(TabIndex section, String searchText)
	{
		switchTab(TabIndex.Search);
		((SearchPanel)getComponentAt(TabIndex.Search.getIndex())).startSearch(section.getIndex(), searchText);
	}

	/**
	 * Skifter til det valgte tab ud fra et index
	 * @param section Indexet for tabben
	 */
	public void switchTab(TabIndex section)
	{
		int index = TabIndex.getIndexFromTabIndex(section);
		setSelectedIndex(index);
	}

	/**
	 * Skifter til den tab som værdien tilhører og henter objektet
	 * @param section Indexet for tabben
	 * @param no Værdiens ID
	 */
	public void switchTabWithNo(TabIndex section, int no)
	{
		switchTab(section);
		((ViewAbstract)getComponentAt(getSelectedIndex())).openWithNo(no);
	}

	@Override
	public void setSelectedIndex(int index) {
    {
		if(getSelectedIndex() != -1 && getSelectedIndex() != 7)
		{
			if(((ViewAbstract)getComponentAt(getSelectedIndex())).confirmTabChange() == true)
			{
				super.setSelectedIndex(index);
			}
		}
		else
		{
			super.setSelectedIndex(index);
		}
	}
}
}